package net.crowdloud.client.user;

import java.util.List;

import net.crowdloud.shared.entities.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class UserProfileList extends FlexTable {

	public UserProfileList(List<User> users) {

		setBorderWidth(1);
		setWidth("900px");

		setCellPadding(5);

		gridColumns();

		gridRows(users);
	}

	private void gridColumns() {

		this.setHTML(0, 0, "<B>Name</B>");
		this.getColumnFormatter().setWidth(0, "200px");

		this.setHTML(0, 1, "<B>Contact</B>");
		this.getColumnFormatter().setWidth(1, "450px");

		this.setHTML(0, 2, "<B>Created</B>");
		this.getColumnFormatter().setWidth(2, "150px");

		this.setHTML(0, 3, "<B>Status</B>");
		this.getColumnFormatter().setWidth(5, "50px");

		this.setHTML(0, 4, " ");
		this.getColumnFormatter().setWidth(6, "50px");
	}

	private void gridRows(List<User> users) {

		if (users != null) {

			int row = 0;
			for (User user : users) {

				row++;

				this.setText(row, 0, user.getName());
				this.setText(row, 1, user.getContact());
				this.setText(row, 2, "");

				/*
				 * String priority = ""; if (backlogEntry.getBacklogPriority()
				 * != null) { priority =
				 * backlogEntry.getBacklogPriority().toString(); }
				 */
				this.setText(row, 3, "");

				HorizontalPanel hp = new HorizontalPanel();
				hp.setSpacing(10);
				hp.add(iconEdit(user));
				hp.add(iconDelete(user));

				this.setWidget(row, 4, hp);

			}
		}
	}

	private static HTML iconEdit(final User user) {

		HTML img = new HTML("<a href='#'><img src='images/edit.gif'></a>");
		img.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				RootPanel.get().clear();
				RootPanel.get().add(new UserPage(user, null));
			}
		});

		img.setTitle("Edit User");

		return img;
	}

	private static HTML iconDelete(final User user) {

		HTML img = new HTML("<a href='#'><img src='images/delete.gif'></a>");
		img.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				// new EditFeatureDescription(classe);
			}
		});

		img.setTitle("Delete User");

		return img;
	}
}
